﻿using System;
using System.Collections.Generic;
using System.Collections;

namespace Diary.BAL
{
    public class ContentTagMap
    {
        const string TableName = "ContentTagMap";

        /// <summary>
        /// 向数据库中插入Category数据，如成功返回新记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        static public Model.Category Insert(Model.Category model)
        {
            string sql = string.Format("insert into {0} (Name, ParentId, HasChild, Type, ShowOrder, Description, Remark) values ('{1}', {2}, {3}, '{4}', {5}, '{6}', '{7}')",
                    TableName, model.Name, model.ParentId, model.HasChild, model.Type, model.ShowOrder, model.Description, model.Remark);
            model.Id = DBController.Default.executeReturnLastId(sql);
            return model;
        }

        /// <summary>
        /// 删除指定ID的记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        static public bool Delete(int id)
        {
            string sql = string.Format("delete from {0} where Id={1}", TableName, id);
            return DBController.Default.executeNoReturn(sql);
        }


        /// <summary>
        /// 修改指定ID的记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        static public bool Modify(Model.Category model)
        {
            string sql = string.Format("update {0} set Name='{2}', ParentId={3}, HasChild={4}, Type='{5}', ShowOrder={6}, Description='{7}', Remark='{8}' where Id={1}",
                    TableName, model.Id, model.Name, model.ParentId, model.HasChild, model.Type, model.ShowOrder, model.Description, model.Remark);
            return DBController.Default.executeNoReturn(sql);
        }

        /// <summary>
        /// 返回所有记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        static public List<Model.Category> query_all()
        {
            string sql = string.Format("select * from {0}", TableName);
            List<Hashtable> results = DBController.Default.query(sql);
            List<Model.Category> history = new List<Model.Category>();
            foreach (Hashtable result in results)
            {
                Model.Category model = new Model.Category();
                model.Id = int.Parse(result["Id"].ToString());
                model.Name = result["Name"].ToString();
                model.ParentId = int.Parse(result["ParentId"].ToString());
                model.HasChild = int.Parse(result["HasChild"].ToString()) == 1;
                model.Type = result["Type"].ToString();
                model.ShowOrder = int.Parse(result["ShowOrder"].ToString());
                model.Description = result["Description"].ToString();
                model.Remark = result["Remark"].ToString();
                history.Add(model);
            }

            return history;
        }
    }
}
